package com.draven.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.draven.common.BusinessException;
import com.draven.entity.SysUser;
import com.draven.mapper.SysUserMapper;
import com.draven.service.SysUserService;
import com.draven.util.JwtUtil;
import com.draven.util.PasswordUtil;
import com.draven.vo.LoginVO;
import com.draven.vo.UserInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户服务实现类
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Autowired
    private JwtUtil jwtUtil;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long register(String username, String password, String email) {
        // 检查用户名是否已存在
        SysUser existUser = getByUsername(username);
        if (existUser != null) {
            throw new BusinessException(400, "用户名已存在");
        }

        // 创建用户
        SysUser user = new SysUser();
        user.setUsername(username);
        user.setPassword(PasswordUtil.encode(password));
        user.setEmail(email);
        user.setStatus(1);
        
        // 保存用户
        save(user);
        
        return user.getId();
    }

    @Override
    public LoginVO login(String username, String password) {
        // 根据用户名查询用户
        SysUser user = getByUsername(username);
        if (user == null) {
            throw new BusinessException(400, "用户名或密码错误");
        }

        // 检查用户状态
        if (user.getStatus() == 0) {
            throw new BusinessException(400, "账号已停用");
        }

        // 校验密码
        if (!PasswordUtil.matches(password, user.getPassword())) {
            throw new BusinessException(400, "用户名或密码错误");
        }

        // 生成token
        String token = jwtUtil.generateToken(user.getId(), user.getUsername());

        // 返回登录信息
        return new LoginVO(user.getId(), user.getUsername(), token);
    }

    @Override
    public UserInfoVO getUserInfo(Long userId) {
        // 查询用户
        SysUser user = getById(userId);
        if (user == null) {
            throw new BusinessException(400, "用户不存在");
        }

        // 转换为VO
        UserInfoVO userInfoVO = new UserInfoVO();
        userInfoVO.setUserId(user.getId());
        userInfoVO.setUsername(user.getUsername());
        userInfoVO.setNickname(user.getNickname());
        userInfoVO.setEmail(user.getEmail());
        userInfoVO.setPhone(user.getPhone());

        return userInfoVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePassword(Long userId, String oldPassword, String newPassword) {
        // 查询用户
        SysUser user = getById(userId);
        if (user == null) {
            throw new BusinessException(400, "用户不存在");
        }

        // 校验旧密码
        if (!PasswordUtil.matches(oldPassword, user.getPassword())) {
            throw new BusinessException(400, "原密码错误");
        }

        // 更新密码
        user.setPassword(PasswordUtil.encode(newPassword));
        updateById(user);
    }

    @Override
    public SysUser getByUsername(String username) {
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUser::getUsername, username);
        return getOne(wrapper);
    }
} 